package com.chapter14.demo2;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
 * 映射
 */
public class MapStream {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,3,5,7);
        //将流中的元素映射为他的平方
        Stream<Integer> stream1 = list.stream();
        stream1.map(x->x*x).forEach(x-> System.out.printf("%d ",x));
        System.out.println();


        Stream<String> stream2 = Stream.of("one", "two", "three");
        //将流中的元素映射为带数字序号的元素
        AtomicInteger index = new AtomicInteger(1);
        stream2.map(x->index.getAndIncrement()+"."+x)
                .forEach(x-> System.out.printf("%s ",x));
    }
}
